package nc;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-10-15 14:51
 **/
public class NC105 {
    public int search (int[] nums, int target) {
        //数组为空直接返回
        if (nums.length == 0) {
            return -1;
        }
        int low = 0;
        int high = nums.length;
        while (low <= high) {
            int mid = low + high >> 1;
            if (nums[mid] == target) {
                //找到第一个符合target的值
                while (mid > 0 && nums[mid] == nums[mid - 1]) {
                    mid = mid - 1;
                }
                return mid;
            } else if (nums[mid] > target) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }
}
